using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using static GameLogic.SocketDefinition;

namespace GameLogic
{
    public enum UserExtraInfoKey
    {
        //登录
        LOGIN = 1001,
        //位置信息
        POSITION = 1014
    }

    /// <summary>
    /// 代理相关的消息ID及包体
    /// </summary>
    public enum API_ID_AGENT : uint
    {
        // 消息服务
        MSG_SERVICE = BS_MSG_AGENT + 0x10,
        // 通知服务器更新账号安全相关数据
        USER_UPDATE_NOTICE = BS_MSG_AGENT + 0x11,
        // 官方实名认证
        CHECK_OC = BS_MSG_AGENT + 0x2a,
        // 个人数据变化
        GROW_CHANGED = BS_MSG_AGENT + 0x21,
        // JS更新提示
        ON_JS_UPDATE = BS_MSG_AGENT + 0x30,
        // 用户数据推送
        ON_USERDATA_UPDATE = BS_MSG_AGENT + 0x24,
        // 代理切换通知
        ON_SOCKET_CHANGE = BS_MSG_AGENT + 0x27,
        // 用户额外信息推送
        ON_USER_EXTRA_INFO = BS_MSG_AGENT + 0x28,
        // 获取断线重连信息
        GET_RECONNECT_INFO = BS_MSG_AGENT + 0x29,
        // 反馈-获取断线重连信息
        ON_GET_RECONNECT_INFO = GAME_REF + (BS_MSG_AGENT + 0x29),
        // 推送装扮消息
        DRESS_INFO = BS_MSG_AGENT + 0x31,
        // 推送数值信息
        AGENT_DATA_PUSH_INFO = BS_MSG_AGENT + 0x32,
        // 推送消息信息
        MSG_INFO = BS_MSG_AGENT + 0x33,
        // 修改三方头像
        MODIFY_USER_FACE = BS_MSG_AGENT + 0x7,
        // 反馈-修改三方头像
        ON_MODIFY_USER_FACE = GAME_REF + (BS_MSG_AGENT + 0x7),
        // 获取redis数据
        GET_REDIS_DATA = BS_MSG_AGENT + 0x34,
        // 反馈-获取redis数据
        ON_GET_REDIS_DATA = GAME_REF + (BS_MSG_AGENT + 0x34)
    }

    #region 用户额外信息推送包
    public class OnUserExtraInfoPacket : ReceivePack
    {
        public uint key;
        public uint count;
        public SuffixIdxReceive suffixIdx = new SuffixIdxReceive();

        public override List<(TypeInfo type, string propertyName)> PropertySequence => new()
        {
            (D_TYPE.DWORD, nameof(key)),
            (D_TYPE.DWORD, nameof(count)),
            (suffixIdx, nameof(suffixIdx))
        };

        public override ReceivePack Read(byte[] data)
        {
            base.Read(data);
            (int offset, int size) = ((int)(suffixIdx.offset - NewHeadReceive.Size), (int)suffixIdx.size);
            if (size != 0)
            {
                var reader = new BinaryPacketReader(data[offset..(offset + size)]);
                string content = reader.ReadString(size);
                if (!string.IsNullOrEmpty(content))
                {
                    if (key == (uint)UserExtraInfoKey.LOGIN)
                    {
                        loginData = JsonConvert.DeserializeObject<LoginData>(content);
                    }
                    else if (key == (uint)UserExtraInfoKey.POSITION)
                    {
                        positioningData = JsonConvert.DeserializeObject<PositioningData>(content);
                    }
                }
            }
            return this;
        }

        public LoginData loginData;

        public PositioningData positioningData;

        [Serializable]
        public class LoginData
        {
            public int namelogin;
            public int fmbank;
            public int regplatform;
            public string regdate;
            public string regtype;
            public string wechatface;
            public string wechatname;
            public int cannamelogin;
        }

        [Serializable]
        public class PositioningData
        {
            public string district;
        }
    }
    #endregion

    #region 推送数值信息
    public class OnAgentDataPushPacket : ReceivePack
    {
        public SuffixIdxReceive suffixIdx = new SuffixIdxReceive();
        public string extraData;

        public override List<(TypeInfo type, string propertyName)> PropertySequence => new()
        {
            (suffixIdx, nameof(suffixIdx))
        };

        public override ReceivePack Read(byte[] data)
        {
            base.Read(data);
            (int offset, int size) = ((int)(suffixIdx.offset - NewHeadReceive.Size), (int)suffixIdx.size);
            if (size != 0)
            {
                var reader = new BinaryPacketReader(data[offset..(offset + size)]);
                extraData = reader.ReadString(size);
            }
            return this;
        }
    }
    #endregion

    #region 个人数据变化
    public class GrowChangedPakcet : ReceivePack
    {
        public uint propertyCount;
        public SuffixIdxReceive suffixIdx = new SuffixIdxReceive();
        public List<GrowProperty> propertyList;

        public override List<(TypeInfo type, string propertyName)> PropertySequence => new()
        {
            (D_TYPE.DWORD, nameof(propertyCount)),
            (suffixIdx, nameof(suffixIdx))
        };

        public override ReceivePack Read(byte[] data)
        {
            base.Read(data);
            for (int i = 0; i < propertyCount; i++)
            {
                GrowProperty gp = new GrowProperty();
                gp.Read(data[(APILength + i * gp.APILength)..gp.APILength]);
                propertyList.Add(gp);
            }
            return this;
        }
    }

    public class GrowProperty : ReceivePack
    {
        public uint propertyId;
        public int oldValue;
        public int newValue;

        public override List<(TypeInfo type, string propertyName)> PropertySequence => new()
        {
            (D_TYPE.DWORD, nameof(propertyId)),
            (D_TYPE.INT, nameof(oldValue)),
            (D_TYPE.INT, nameof(newValue))
        };
    }
    #endregion

    public class DressInfoPakcet : ReceivePack
    {
        public uint result;
        public uint userID;
        public SuffixIdxReceive suffixIdx = new SuffixIdxReceive();
        public string dressJsonStr;

        public override List<(TypeInfo type, string propertyName)> PropertySequence => new()
        {
            (D_TYPE.DWORD, nameof(result)),
            (D_TYPE.DWORD, nameof(userID)),
            (suffixIdx, nameof(suffixIdx))
        };

        public override ReceivePack Read(byte[] data)
        {
            base.Read(data);
            (int offset, int size) = ((int)(suffixIdx.offset - NewHeadReceive.Size), (int)suffixIdx.size);
            if (size != 0)
            {
                var reader = new BinaryPacketReader(data[offset..(offset + size)]);
                dressJsonStr = reader.ReadString(size);
            }
            return this;
        }
    }
}